package safari.controller.admin;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import safari.model.admin.AdminService;
import safari.model.member.MemberVO;
import safari.model.sell.TradeHistoryVO;

public class AdminController extends MultiActionController {
	private AdminService service;
	//AdminController 생성자
	public AdminController(AdminService service) {
		super();
		this.service = service;
	}
	/**
	 * getAllMember()
	 * 모든 회원을 조회하는 method
	 * return type : List<>
	 * ajax(Json View) 사용
	 */
	public ModelAndView getAllMember(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		List<MemberVO> mList = service.getAllMember();
		System.out.println(mList);
		return new ModelAndView("JsonView","mList", mList);
	}
	/**
	 * findMemberById()
	 * 아이디로 회원을 검색하는 method
	 * return type : MemberVO
	 * ajax(Json View) 사용
	 */
	public ModelAndView findMemberById(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String id = request.getParameter("adminId");
		MemberVO mvo = service.findMemberById(id);
		return new ModelAndView("JsonView","mvo", mvo);
	}
	/**
	 * findMemberByName()
	 * 모든 회원을 조회하는 method
	 * return type : List<>
	 * ajax(Json View) 사용
	 */
	public ModelAndView findMemberByName(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String name = request.getParameter("adminName");
		List<MemberVO> mList = service.findMemberByName(name);
		return new ModelAndView("JsonView","mList", mList);
	}
	public String moveToFind(HttpServletRequest request, HttpServletResponse response)throws SQLException{
		return  "admin/findMember";
	}
	public String moveToMain(HttpServletRequest request, HttpServletResponse response, HttpSession session)throws SQLException{
		String path="";
		MemberVO mvo = (MemberVO) session.getAttribute("userInfo");
		if(mvo==null||!mvo.getGrade().equals("admin")){
			path="admin/notAdmin";
		}else{
			path="admin/main";
		}
		return  path;
	}
	/**
	 * findMemberByAddress()
	 * 모든 회원을 조회하는 method
	 * return type : List<>
	 * ajax(Json View) 사용
	 */
	public ModelAndView findMemberByAddress(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String address = request.getParameter("adminAddress");
		List<MemberVO> mList = service.findMemberByAddress(address);
		return new ModelAndView("JsonView","mList", mList);
	}
	/**
	 * getAllTradedHistory()
	 * 모든 거래 내역을 보여주는 method
	 * return type : List<>
	 * admin_getAllHistroy.jsp로 이동하여 출력
	 */
	public ModelAndView getAllTradedHistory(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		List<TradeHistoryVO> thList = service.getAllTradedHistory();
		System.out.println(thList);
		return new ModelAndView("admin/getAllHistory","thList", thList);
	}
}
